package com.shouhe.mysap.controller.pur;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.SelectGroupResult;
import com.shouhe.mysap.entity.PurPerson;
import com.shouhe.mysap.entity.TechPerson;
import com.shouhe.mysap.service.PurPersonService;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/pur-person")
public class PurPersonController {

    @Autowired
    private PurPersonService purPersonService;

    /**
     * @描述 得到全部的采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurPersons")
    public PageResult getPurPersons(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, purPersonService);
        return pageResult;
    }

    /**
     * @描述 通过采购组得到采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurPersonByPurGroup")
    public PurPerson getPurPersonByPurGroup(Integer PurGroup) {
        PurPerson PurPerson = SelectUtil.selectOneEq("pur_group", PurGroup, purPersonService);
        return PurPerson;
    }

    /**
     * @描述 通过名字得到采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurPersonByPurName")
    public PurPerson getPurPersonByPurName(String PurName) {
        PurPerson PurPerson = SelectUtil.selectOneEq("pur_name", PurName, purPersonService);
        return PurPerson;
    }

    /**
     * @描述 得到全部技术组
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurGroupResults")
    public List<SelectGroupResult> getPurGroupResults() {
        List<SelectGroupResult> groups = new ArrayList<>();
        List<PurPerson> list = purPersonService.list(null);
        for (PurPerson person : list) {
            SelectGroupResult result = new SelectGroupResult();
            result.setContent(person.getPurGroup() + "组");
            result.setValue(person.getPurGroup() + "");
            groups.add(result);
        }
        //如果是 添加 则默认选择一个技术组
        if (groups.size() > 0) {
            groups.get(0).setSelected(true);
        }
        return groups;
    }




    /**
     * @描述 添加一个采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/addPurPerson")
    public MySapResult addPurPerson(PurPerson PurPerson) {
        //首先判断采购组是否已经存在
        PurPerson person = getPurPersonByPurGroup(PurPerson.getPurGroup());
        if (person != null) {
            return MySapResult.fail("添加采购员失败,采购组已经存在");
        }
        return AddUtil.addObject("添加采购员", PurPerson, purPersonService);
    }


    /**
     * @描述 通过id删除 采购员
     * @参数
     * @返回值
     */
    @RequestMapping("/updatePurPerson")
    public MySapResult updatePurPerson(PurPerson PurPerson) {
        //首先判断采购组是否已经存在
        PurPerson person = getPurPersonByPurGroup(PurPerson.getPurGroup());
        if (person != null) {
            return MySapResult.fail("更新采购员失败,采购组已经存在");
        }
        return UpdateUtil.updateObjectById("更新采购员", PurPerson, purPersonService);
    }


    /**
     * @描述 通过 id数组 删除采购员
     * @参数
     * @返回值
     */
    @RequestMapping("/deletePurPersons")
    public MySapResult deletePurPersons(@RequestParam("ids[]") Integer[] ids) {
        return DeleteUtil.deleteMany("删除采购员", ids, purPersonService);
    }


}

